<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Luadocs for parser.lua</title>
    <link rel="stylesheet" href="../luadoc.css" type="text/css" />
	<!--meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/-->
</head>

<body>
<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->

<div id="main">

<div id="navigation">


<h1>LuaDoc</h1>
<ul>
	
	<li><a href="../index.html">Index</a></li>
	
</ul>


<!-- Module list -->

<h1>Modules</h1>
<ul>

	<li>
		<a href="../modules/base.html">base</a>
	</li>

	<li>
		<a href="../modules/bin.html">bin</a>
	</li>

	<li>
		<a href="../modules/debug.html">debug</a>
	</li>

	<li>
		<a href="../modules/fstable.html">fstable</a>
	</li>

	<li>
		<a href="../modules/getopt.html">getopt</a>
	</li>

	<li>
		<a href="../modules/io.html">io</a>
	</li>

	<li>
		<a href="../modules/lcs.html">lcs</a>
	</li>

	<li>
		<a href="../modules/list.html">list</a>
	</li>

	<li>
		<a href="../modules/math.html">math</a>
	</li>

	<li>
		<a href="../modules/mbox.html">mbox</a>
	</li>

	<li>
		<a href="../modules/object.html">object</a>
	</li>

	<li>
		<a href="../modules/package.html">package</a>
	</li>

	<li>
		<a href="../modules/parser.html">parser</a>
	</li>

	<li>
		<a href="../modules/set.html">set</a>
	</li>

	<li>
		<a href="../modules/std.html">std</a>
	</li>

	<li>
		<a href="../modules/strbuf.html">strbuf</a>
	</li>

	<li>
		<a href="../modules/string.html">string</a>
	</li>

	<li>
		<a href="../modules/table.html">table</a>
	</li>

	<li>
		<a href="../modules/tree.html">tree</a>
	</li>

</ul>



<!-- File list -->

<h1>Files</h1>
<ul>

	<li>
		<a href="../files/base.html">base.lua</a>
	</li>

	<li>
		<a href="../files/bin.html">bin.lua</a>
	</li>

	<li>
		<a href="../files/debug_ext.html">debug_ext.lua</a>
	</li>

	<li>
		<a href="../files/debug_init.html">debug_init.lua</a>
	</li>

	<li>
		<a href="../files/fstable.html">fstable.lua</a>
	</li>

	<li>
		<a href="../files/getopt.html">getopt.lua</a>
	</li>

	<li>
		<a href="../files/io_ext.html">io_ext.lua</a>
	</li>

	<li>
		<a href="../files/lcs.html">lcs.lua</a>
	</li>

	<li>
		<a href="../files/list.html">list.lua</a>
	</li>

	<li>
		<a href="../files/math_ext.html">math_ext.lua</a>
	</li>

	<li>
		<a href="../files/mbox.html">mbox.lua</a>
	</li>

	<li>
		<a href="../files/modules.html">modules.lua</a>
	</li>

	<li>
		<a href="../files/object.html">object.lua</a>
	</li>

	<li>
		<a href="../files/package_ext.html">package_ext.lua</a>
	</li>

	<li><strong>parser.lua</strong></li>
	
	<li>
		<a href="../files/set.html">set.lua</a>
	</li>

	<li>
		<a href="../files/std.html">std.lua</a>
	</li>

	<li>
		<a href="../files/strbuf.html">strbuf.lua</a>
	</li>

	<li>
		<a href="../files/strict.html">strict.lua</a>
	</li>

	<li>
		<a href="../files/string_ext.html">string_ext.lua</a>
	</li>

	<li>
		<a href="../files/table_ext.html">table_ext.lua</a>
	</li>

	<li>
		<a href="../files/tree.html">tree.lua</a>
	</li>

	<li>
		<a href="../files/xml.html">xml.lua</a>
	</li>

</ul>






</div> <!-- id="navigation" -->

<div id="content">

<h1>File <code>parser.lua</code></h1>


<p>Parser generator. <p>A parser is created by</p> <blockquote> <p><code>p = Parser {grammar}</code></p> </blockquote> <p>and called with</p> <blockquote> <p><code>result = p:parse (start_token, token_list[, from])</code></p> </blockquote> <p>where start_token is the non-terminal at which to start parsing in the grammar, token_list is a list of tokens of the form</p> <blockquote> <p><code>{ty = "token_type", tok = "token_text"}</code></p> </blockquote> <p>and from is the token in the list from which to start (the default value is 1).</p> <p>The output of the parser is a tree, each of whose nodes is of the form:</p> <blockquote> <p><code>{ty = symbol, node<sub>1</sub> = tree<sub>1</sub>, node<sub>2</sub> = tree<sub>2</sub>, ... [, list]}</code></p> </blockquote> <p>where each <code>node<sub>i</sub></code> is a symbolic name, and list is the list of trees returned if the corresponding token was a list token.</p> <p>A grammar is a table of rules of the form</p> <blockquote> <p><code>non-terminal = {production<sub>1</sub>, production<sub>2</sub>, ...}</code></p> </blockquote> <p>plus a special item</p> <blockquote> <p><code>lexemes = Set {"class<sub>1</sub>", "class<sub>2</sub>", ...}</code></p> </blockquote> <p>Each production gives a form that a non-terminal may take. A production has the form</p> <blockquote> <p><code>production = {"token<sub>1</sub>", "token<sub>2</sub>", ..., [action][,abstract]}</code></p> </blockquote> <p>A production</p> <ul> <li>must not start with the non-terminal being defined (it must not be left-recursive)</li> <li>must not be a prefix of a later production in the same non-terminal</li> </ul> <p>Each token may be</p> <ul> <li>a non-terminal, i.e. a token defined by the grammar</li> <ul> <li>an optional symbol is indicated by the suffix <code>_opt</code></li> <li>a list is indicated by the suffix <code>_list</code>, and may be followed by <code>_&le;separator-symbol&gt;</code> (default is no separator)</li> </ul> <li>a lexeme class</li> <li>a string to match literally</li> </ul> <p>The parse tree for a literal string or lexeme class is the string that was matched. The parse tree for a non-terminal is a table of the form</p> <blockquote> <p><code>{ty = "non_terminal_name", tree<sub>1</sub>, tree<sub>2</sub>, ...}</code></p> </blockquote> <p>where the <code>tree<sub>i</sub></code> are the parse trees for the corresponding terminals and non-terminals.</p> <p>An action is of the form</p> <blockquote> <p><code>action = function (tree, token, pos) ... return tree_ end</code></p> </blockquote> <p>It is passed the parse tree for the current node, the token list, and the current position in the token list, and returns a new parse tree.</p> <p>An abstract syntax rule is of the form</p> <blockquote> <p><code>name = {i<sub>1</sub>, i<sub>2</sub>, ...}</code></p> </blockquote> <p>where <code>i<sub>1</sub></code>, <code>i<sub>2</sub></code>, ... are numbers. This results in a parse tree of the form</p> <blockquote> <p><code>{ty = "name"; tree<sub>i<sub>1</sub></sub>, tree<sub>i<sub>2</sub></sub>, ...}</code></p> </blockquote> <p>If a production has no abstract syntax rule, the result is the parse node for the current node.</p> <p>FIXME: Give lexemes as an extra argument to <code>Parser</code>? <br>FIXME: Rename second argument to parse method to "tokens"? <br>FIXME: Make start_token an optional argument to parse? (swap with token list) and have it default to the first non-terminal?</p></p>






<h2>Functions</h2>
<table class="function_list">

	<tr>
	<td class="name" nowrap><a href="#Parser:_clone">Parser:_clone</a>&nbsp;(grammar)</td>
	<td class="summary">Parser constructor </td>
	</tr>

	<tr>
	<td class="name" nowrap><a href="#Parser:parse">Parser:parse</a>&nbsp;(start, token, from)</td>
	<td class="summary">Parse a token list.</td>
	</tr>

</table>






<br/>
<br/>




<h2><a name="functions"></a>Functions</h2>
<dl class="function">



<dt><a name="Parser:_clone"></a><strong>Parser:_clone</strong>&nbsp;(grammar)</dt>
<dd>
Parser constructor


<h3>Parameters</h3>
<ul>
	
	<li>
	  grammar: parser grammar
	</li>
	
</ul>






<h3>Return value:</h3>
parser



</dd>




<dt><a name="Parser:parse"></a><strong>Parser:parse</strong>&nbsp;(start, token, from)</dt>
<dd>
Parse a token list.


<h3>Parameters</h3>
<ul>
	
	<li>
	  start: the token at which to start
	</li>
	
	<li>
	  token: the list of tokens
	</li>
	
	<li>
	  from: the index of the token to start from (default: 1)
	</li>
	
</ul>






<h3>Return value:</h3>
parse tree



</dd>


</dl>







</div> <!-- id="content" -->

</div> <!-- id="main" -->

<div id="about">
	<p><a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0!" height="31" width="88" /></a></p>
</div> <!-- id="about" -->

</div> <!-- id="container" -->
</body>
</html>
